package com.shop.user.api.controller;

import com.shop.common.entity.user.Address;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.user.AddressInput;
import com.shop.common.model.result.user.AddressOutput;
import com.shop.common.tmpl.BaseController;
import com.shop.common.util.SessionUtils;
import com.shop.common.util.excption.UserNotFoundException;
import com.shop.user.api.api.IAddressApi;
import com.shop.user.api.service.IAddressSvc;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user/address")
public class AddressController extends BaseController implements IAddressApi {
    static final Logger LOGGER = LoggerFactory.getLogger(AddressController.class);

    @Autowired
    IAddressSvc addressService;
    @Autowired
    SessionUtils sessionUtils;

    @Override
    public String apiAddAddress(@RequestBody AddressInput address, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        address.setUserId(userId);
        address.setType(2);
        ServiceBean serviceBean = addressService.insertAddress(new InputBean<AddressInput>(address));
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed();
    }

    @Override
    public String apiGetAddress(Integer id) {
        AddressInput address = new AddressInput();
        address.setId(id);
        ServiceBean<Address> serviceBean = addressService.selectAddress(newParamMode(address));
        if (!serviceBean.isSuccess()) {
            return returnFailed();
        }
        return returnData(serviceBean.getData());
    }

    @Override
    public String apiGetAddressList(AddressInput address, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        address.setUserId(userId);
        ServiceBean<List<AddressOutput>> serviceBean = addressService.selectAddressList(newParamMode(address));
        if (serviceBean.isSuccess()) {
            List<AddressOutput> addressList = serviceBean.getData();
            return returnData(addressList);
        }
        return returnFailed();
    }

    @Override
    public String apiUpdateAddress(@RequestBody AddressInput address, HttpServletRequest request) {
        ServiceBean serviceBean = addressService.updateAddress(newParamMode(address));
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed();
    }

    @Override
    public String settingDefault(String id, HttpServletRequest request) throws UserNotFoundException {
        if (StringUtils.isBlank(id)) {
            return returnFailed("参数错误");
        }
        Integer userId = sessionUtils.USER_ID(request);
        AddressInput address = new AddressInput();
        address.setUserId(userId);
        address.setId(Integer.valueOf(id));
        ServiceBean serviceBean = addressService.settingDefault(address);
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed(serviceBean.getMsg());
    }

    @Override
    public String apiDeleteAddress(String id) {
        if (StringUtils.isBlank(id)) {
            return returnFailed("参数错误");
        }
        List<Integer> list = new ArrayList<>();
        list.add(Integer.valueOf(id));
        ServiceBean serviceBean = addressService.deleteAddressById(newParamMode(list));
        if (serviceBean.isSuccess()) {
            return returnSuccess();
        }
        return returnFailed(serviceBean.getMsg());
    }
}
